package com.dust.web.controller.monitor;

import com.dust.common.annotation.Log;
import com.dust.common.core.page.TableDataInfo;
import com.dust.system.domain.SysOperateLog;
import com.dust.common.core.controller.BaseController;
import com.dust.common.core.domain.AjaxResult;
import com.dust.common.enums.BusinessType;
import com.dust.common.utils.poi.ExcelUtil;
import com.dust.system.service.ISysOperateLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 操作日志记录
 */
@Controller
@RequestMapping("/monitor/operate/log")
public class SysOperateLogController extends BaseController {
    private final String prefix = "monitor/operate/log";

    @Autowired
    private ISysOperateLogService operateLogService;

    @RequiresPermissions("monitor:operate:log:view")
    @GetMapping()
    public String operateLog() {
        return prefix + "/log";
    }

    @RequiresPermissions("monitor:operate:log:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysOperateLog operateLog) {
        startPage();
        List<SysOperateLog> list = operateLogService.selectOperateLogList(operateLog);
        return getDataTable(list);
    }

    @Log(title = "操作日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("monitor:operate:log:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysOperateLog operateLog) {
        List<SysOperateLog> list = operateLogService.selectOperateLogList(operateLog);
        ExcelUtil<SysOperateLog> util = new ExcelUtil<>(SysOperateLog.class);
        return util.exportExcel(list, "操作日志");
    }

    @RequiresPermissions("monitor:operate:log:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(operateLogService.deleteOperateLogByIds(ids));
    }

    @RequiresPermissions("monitor:operate:log:detail")
    @GetMapping("/detail/{operateId}")
    public String detail(@PathVariable("operateId") Long operateId, ModelMap mmap) {
        mmap.put("operateLog", operateLogService.selectOperateLogById(operateId));
        return prefix + "/detail";
    }

    @Log(title = "操作日志", businessType = BusinessType.CLEAN)
    @RequiresPermissions("monitor:operate:log:remove")
    @PostMapping("/clean")
    @ResponseBody
    public AjaxResult clean() {
        operateLogService.cleanOperateLog();
        return success();
    }
}
